package com.nms.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.nms.handler.SystemInfoHandler;
import com.nms.model.Comment;
import com.nms.model.News;
import com.nms.model.NewsType;
import com.nms.model.User;
import com.nms.model.pojo.PageParam;
import com.nms.model.pojo.SystemInfo;
import com.nms.service.CKEditorService;
import com.nms.service.DictionaryService;
import com.nms.service.NewsService;
import com.nms.service.UserService;
import com.nms.util.SystemUtil;
import com.nms.util.UserLoginUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class NewsManageAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SystemInfo systemInfo;
	private News news;
	private UserService userService;
	private CKEditorService ckEditorService;
	private List<News> newsList;
	private PageParam pageParam;
	private Comment comment;
	private List<NewsType> newsTypes;
	private NewsService newsService;
	private DictionaryService dictionaryService;

	public NewsManageAction() {
		systemInfo = new SystemInfo();
		pageParam = new PageParam();
		SystemInfoHandler.getInstance().copySystemInfo(systemInfo);
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public PageParam getPageParam() {
		return pageParam;
	}

	public void setPageParam(PageParam pageParam) {
		this.pageParam = pageParam;
	}

	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public List<NewsType> getNewsTypes() {
		return newsTypes;
	}

	public void setNewsTypes(List<NewsType> newsTypes) {
		this.newsTypes = newsTypes;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
		newsTypes = dictionaryService.findNewsTypes();
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CKEditorService getCkEditorService() {
		return ckEditorService;
	}

	public void setCkEditorService(CKEditorService ckEditorService) {
		this.ckEditorService = ckEditorService;
	}

	// ////////////////////////////////////////

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public SystemInfo getSystemInfo() {
		return systemInfo;
	}

	public void setSystemInfo(SystemInfo systemInfo) {
		this.systemInfo = systemInfo;
	}

	public String open() {
		/* 获得HttpServletRequest对象 */
		HttpServletRequest hsr = ServletActionContext.getRequest();
		/* 获得请求的参数 */
		String id = hsr.getParameter("id");
		/* 生成要浏览的新闻文件路径 */
		String url = SystemUtil.getInstance().getAppDir(hsr)
				+ SystemUtil.contentBasePath + id + ".news";
		/* 从数据库中找出要浏览的新闻对象 */
		news = newsService.findById(id);
		if (news != null) {
			/* 将新闻文件中的内容添加到新闻对象中 */
			news.setContent(ckEditorService.get(url));
			return "open";
		}
		return "error";
	}

	public String show() {
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		/* 获得HttpServletRequest对象 */
		HttpServletRequest hsr = ServletActionContext.getRequest();
		/* 获得请求的参数 */
		String size = hsr.getParameter("s");
		String index = hsr.getParameter("i");
		System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		String order = hsr.getParameter("o");
		String degree = hsr.getParameter("d");
		String type1 = hsr.getParameter("t1");
		String type2 = hsr.getParameter("t2");
		String type3 = hsr.getParameter("t3");
		String publishTime = hsr.getParameter("pt");
		String activeTime = hsr.getParameter("at");

		try {
			if(index != null){
				pageParam.setIndex(Integer.parseInt(index));
			}
			if (size != null) {
				pageParam.setSize(Integer.parseInt(size));
			}
			if(order != null){
				
			}
			if(degree != null){
				
			}
		} catch (Exception e) {
			return "error";
		}
		pageParam.setAction("newsManage_show.action");
		newsList = newsService.findByPage(pageParam);
		return "show";
	}

	public String delete() {
		return "deleteSuccess";
	}

	public String update() {
		return "updateSuccess";
	}

	public String showAdd() {
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		return "showAdd";
	}

	public String showEdit() {
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		news = (News) ActionContext.getContext().getSession()
				.get("currentNews");
		return "showEdit";
	}

	@Override
	public String execute() {
		/* 判断用户是否登录以及用户权限 */
		if (UserLoginUtil.getInstance().isExceed())
			return "exceed";
		/* 获得HttpServletRequest对象 */
		HttpServletRequest hsr = ServletActionContext.getRequest();
		/* 获得请求的参数 */
		String id = hsr.getParameter("id");
		String act = hsr.getParameter("act");
		/* 根据请求参数生成一个新闻的访问路径 */
		String url = SystemUtil.getInstance().getAppDir(hsr)
				+ SystemUtil.contentBasePath + id + ".news";
		/* 下面为操作逻辑 */
		if (act.equals("delete") && newsService.deleteById(id)) {
			/* 删除新闻后，为新闻的显示加载新闻 */
			newsList = newsService.findAll();
			/* 删除新闻文件 */
			// ckEditorService.delete(url);
			ckEditorService.deleteContent(id);
			return "success";
		} else if (act.equals("update")) {
			/* 从数据库中找到要更新的新闻 */
			News n = newsService.findById(id);
			/* 设置新闻不能被手动更改的属性 */
			news.setPublisher(n.getPublisher());
			news.setActiveTime(n.getActiveTime());
			news.setReleaseTime(n.getReleaseTime());
			news.setRebroadcast(n.getRebroadcast());
			news.setConcernCount(n.getConcernCount());
			news.setCommentCount(n.getCommentCount());
			news.setId(id);

			/* 获得提交过来的新闻内容，将被存储为新闻文件 */
			String content = news.getContent();
			news.setContent(url);
			if (newsService.update(news)) {
				/* 将新闻内容存储为新闻文件 */
				// ckEditorService.save(url, content);
				ckEditorService.saveContent(id, content);
				/* 更新新闻后，为新闻的显示加载新闻 */
				newsList = newsService.findAll();
				return "success";
			} else
				news.setContent(content);
		} else if (act.equals("edit")) {

			/* 查找出将要进行编辑的新闻 */
			news = newsService.findById(id);
			if (news != null) {
				url = SystemUtil.getInstance().getContentPath() + news.getId()
						+ ".news";
				/* 为显示新闻内容加载新闻文件的内容 */
				news.setContent(ckEditorService.get(url));
				/* 将该新闻对象添加到该回话中 */
				ActionContext.getContext().getSession()
						.put("currentNews", news);

				return "edit";
			}
		} else if (act.equals("open")) {
			/* 该功能为做，所以直接提示出错信息 */
			return "error";
		} else if (act.equals("add")) {
			/* 获得当前用户信息 */
			User u = (User) ActionContext.getContext().getSession().get("user");
			/* 给将要添加的新闻设置一些初始属性值，即：不可以人工设置的值 */
			news.setPublisher(u);
			news.setReleaseTime(new Date());
			news.setRebroadcast(0);
			news.setConcernCount(0);
			news.setCommentCount(0);
			String content = news.getContent();
			news.setContent(url);
			if ((id = (String) newsService.add(news)) != null) {
				url = SystemUtil.getInstance().getAppDir(hsr)
						+ SystemUtil.contentBasePath + id + ".news";
				/* 将新闻内容保存问新闻文件 */
				// ckEditorService.save(url, content);
				ckEditorService.saveContent(id, content);

				/* 问新闻管理的显示而加载新闻列表 */
				newsList = newsService.findAll();
				return "success";
			}
		} else if (act.equals("comment")) {
			/* 从数据库中找到要平路的新闻对象 */
			news = newsService.findById(id);
			Set<Comment> cs = news.getComments();
			comment.setNews(news);
			comment.setUser((User) ActionContext.getContext().getSession()
					.get("user"));
			comment.setDate(new Date());
			cs.add(comment);
			news.setComments(cs);
			/* 使评论数加一 */
			news.setCommentCount(news.getCommentCount() + 1);
			/* 更新新闻信息 */
			newsService.update(news);
			url = SystemUtil.getInstance().getContentPath() + news.getId()
					+ ".news";
			/* 为显示新闻内容加载新闻文件的内容 */
			news.setContent(ckEditorService.get(url));
			return "commentSuccess";
		}
		return "error";
	}

	// //////////////////////////////
	protected boolean isExceeded() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User u = (User) session.get("user");
		// if(u.getRole().getId() == 5)
		// return true;
		return (u == null);
	}
}
